package com.pet.shop.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.pet.config.SessionConstants;
import com.pet.shop.entity.order.Order;
import com.pet.shop.entity.user.UserInfo;
import com.pet.shop.service.order.IOrderService;
import com.pet.shop.utils.ApiResultUtil;
import org.apache.catalina.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/api/order")
public class OrderApiController {

    protected static final Logger logger = LoggerFactory.getLogger(OrderApiController.class);

    @Autowired
    private IOrderService orderService;

    /**
     * 添加预约
     * @param request
     * @param order
     * @return
     */
    @RequestMapping("insert")
    public Map<String, Object> insertOrder(HttpServletRequest request, @RequestBody Order order){
        logger.info("酒店预约请求参数：{}", JSONObject.toJSONString(order));
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute(SessionConstants.APP_SESSION_KEY);
        if(userInfo == null){
            return ApiResultUtil.error_param("登录信息不存在，header参数必传");
        }
        order.setCreateTime(new Date());
        order.setCreatorId(userInfo.getId());
        order.setUserId(userInfo.getId());
        orderService.insertEntity(order);
        return ApiResultUtil.success_data(order);
    }

    @RequestMapping("queryList")
    public Map<String, Object> queryList(HttpServletRequest request){
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute(SessionConstants.APP_SESSION_KEY);
        if(userInfo == null){
            return ApiResultUtil.success_data(new ArrayList<>());
        }
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userId", userInfo.getId());
        List<Order> orderList = orderService.queryOrderByParamMap(paramMap);
        return ApiResultUtil.success_data(orderList);
    }


}
